package com.jingdianjichi.subject.common.entity;

import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页返回的实体
 */
@Data
public class PageResult<T>  implements java.io.Serializable{
    /**
     * 默认页数
     */
    private Integer pageNo =1;

    /**
     * 默认每页条数
     */
    private Integer pageSize = 20;

    /**
     * 默认总条数
     */
    private Integer total = 0;

    /**
     * 默认总页数
     */
    private Integer totalPages = 0;

    /**
     * 返回给前端的结果集
     */
    private List<T> result = Collections.emptyList();

    /**
     * 默认的起始页数
     */
    private Integer start;

    /**
     * 默认的结束页数
     */
    private Integer end;

    public void setRecords(List<T> result){
        this.result = result;
        if (result !=null && result.size()>0){
            setTotal(result.size());
        }
    }

    public void setTotal(Integer total){
        this.total = total;
        if(this.pageSize >0){
            this.totalPages = (total/this.pageSize) + (total % this.pageSize ==0?0:1);
        }else{
            this.totalPages = 0;
        }
        this.start = (this.pageSize>0?(this.pageNo-1)* this.pageSize :0) +1;
        this.end = (this.start-1 + this.pageSize* (this.pageNo>0?1:0));

    }
    public void setPageSize(Integer pageSize){
        this.pageSize = pageSize;
    }

    public void setPageNo(Integer pageNo){
        this.pageNo =pageNo;
    }



}
